﻿/**
 * This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of 
 * this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to Creative Commons, 
 * 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 * 
 * Copyright (c) 2010 Colin Duffy
 * 
 */

package net.inctm.components
{
	//_______________________________________________________________________________________________________________
	//------------------------------------------------------------------------------------------------------- IMPORTS
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * Checkbox functionality.
	 *
	 * @author Colin Duffy
	 * @modified Sat Feb 13 2010 17:49:35 GMT-0800 (Pacific Standard Time)
	 */
	public class CheckBox extends MovieClip
	{
		//____________________________________________________________________________________________________________
		//------------------------------------------------------------------------------------------- PUBLIC VARIABLES
		
		
		
		//____________________________________________________________________________________________________________
		//------------------------------------------------------------------------------------------ PRIVATE VARIABLES
		
		private var _selected		:Boolean = false;
		
		//____________________________________________________________________________________________________________
		//------------------------------------------------------------------------------------------------ CONSTRUCTOR
		
		public function CheckBox () : void
		{
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.addEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		
		//____________________________________________________________________________________________________________
		//--------------------------------------------------------------------------------------------- PUBLIC METHODS
		
		
		
		//____________________________________________________________________________________________________________
		//------------------------------------------------------------------------------------------ PROTECTED METHODS
		
		
		
		//____________________________________________________________________________________________________________
		//--------------------------------------------------------------------------------------------- EVENT HANDLERS
		
		protected function dispose (e:Event = null) : void {
			this.removeEventListener(MouseEvent.CLICK, onClick);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		
		//____________________________________________________________________________________________________________
		//--------------------------------------------------------------------------------------------- MOUSE HANDLERS
		
		protected function onClick (e:MouseEvent) : void {
			selected = !_selected;
		}
		
		//____________________________________________________________________________________________________________
		//---------------------------------------------------------------------------------------------------- GETTERS
		
		public function get selected () : Boolean {
			return _selected;
		}
		
		//____________________________________________________________________________________________________________
		//---------------------------------------------------------------------------------------------------- SETTERS
		
		public function set selected (newVal:Boolean) : void {
			_selected = newVal;
			this.dispatchEvent (new ComponentEvent(ComponentEvent.CHANGE));
		}
		
		//____________________________________________________________________________________________________________
		//------------------------------------------------------------------------------------------ INHERITED METHODS
		
		/**
		 * Returns "[CheckBox]" when you call the class name directly.
		 * @return String
		 */
		override public function toString():String {
			return "[CheckBox]";
		}
	}
}